﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Stealth
{
    public class GameObject : EngineObject
    {
        public Vector2 drawPosition = Vector2.Zero;
        public PlayScreen ps;
        public static Texture2D tile = Engine.Load<Texture2D>("images\\tile");

        // aux
        public float gravityAcc = 1f; // não pode ser menor que 1!
        public Vector2 speed = new Vector2(0, 0);
        public bool onGround = false;
        public bool isFacingLeft = false;

        public GameObject(PlayScreen ps, Texture2D sprite)
            : base(sprite)
        {
            this.ps = ps;
        }

        public override void Update(GameTime gameTime)
        {
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (ps.c == null || sprite == null) return;

            drawPosition = position - ps.c.position + ps.c.drawPosition;
            spriteBatch.Draw(sprite, drawPosition, sourceRectangle, Color.White, rotation, origin, scale, isFacingLeft ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);

            //DrawHitBox(spriteBatch);
            //DrawCollidingTiles(spriteBatch);
        }
        
        public void DrawHitBox(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(GameScreenManager.blackScreen,
                             new Vector2((int)(HitBox.Location.X - ps.c.position.X + ps.c.drawPosition.X), (int)(HitBox.Location.Y - ps.c.position.Y + ps.c.drawPosition.Y)),
                             //drawPosition - new Vector2(HitBox.Width, HitBox.Height) / 2, 
                             HitBox, 
                             new Color(0, 0, 0, .5f));
        }

        public void DrawCollidingTiles(SpriteBatch spriteBatch)
        {
            ps.map.GetCollidingTiles(this).ForEach(p => spriteBatch.Draw(tile, 
                                                                            (tile.Width * new Vector2(p.X, p.Y)) - ps.c.position + ps.c.drawPosition,
                                                                            new Color(1, 1, 1, .5f)));
        }

        public virtual void CheckCollisionWithMap(GameTime gameTime)
        {
            speed.Y += gravityAcc; // update gravity

            // update Position.X
            if (speed.X != 0)
            {
                position.X += speed.X;
                float dH = ps.CollisionH(this, speed.X < 0, true);
                if (dH != 0) position.X += dH;
                speed.X = 0;
            }

            // update Position.Y
            position.Y += speed.Y;
            float dV = ps.CollisionV(this, speed.Y < 0, !onGround);
            if (dV != 0)
            {
                position.Y += dV + 0.001f * (speed.Y < 0 ? 1 : -1);
                if (speed.Y > 0)
                    onGround = true; // se estava caindo (não subindo), então agora está no chão pq colidiu (com o chão)
                speed.Y = 0;
            }
            else onGround = false;
        }
    }
}
